package com.fzh.controller.back;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.entity.Images;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.utils.PageUtil;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.ImagesService;
import com.fzh.service.ProductService;

@Controller
@RequestMapping("/back/")
public class ImagesController {

	@Autowired
	private ImagesService imagesService;
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 查询出商品图片数据
	 * @param page
	 * @param limit
	 * @param f_pid
	 * @param isMain
	 * @return
	 */
	@RequestMapping("/queryAllImages")
	@ResponseBody
	public ResponeMessageUtil queryAllImages(Integer page,Integer limit,String f_pid,String isMain) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(imagesService.queryImagesCounts(f_pid,isMain), imagesService.queryAllImages(setValue,limit,f_pid,isMain));
	}
	
	/**
	 * 查询出所有商品数据展示到下拉框
	 * @return
	 */
	@RequestMapping("/queryAllProductWitchImagesSelect")
	@ResponseBody
	public List<Product> queryAllProductWitchImagesSelect(){
		return productService.queryAllProductWitchImagesSelect();
	}
	
	/**
	 * 添加一个商品图片的信息
	 * @param isMainStatus
	 * @param images
	 * @return
	 */
	@RequestMapping("/addImages")
	@ResponseBody
	public Boolean addImages(String isMainStatus,Images images) {
		if(isMainStatus != null && isMainStatus != "" && isMainStatus.equals("on")) {
			images.setIsMain(1);
			productService.editProductImgMainById(images.getF_pid(),images.getImg());
		}else {
			images.setIsMain(0);
		}
		return imagesService.addImages(images);
	}
	
	
	@RequestMapping("/addImagesWitchProductId")
	@ResponseBody
	public Boolean addImagesWitchProductId(Images images) {
		System.out.println(images);
		return false;
	}
	
	/**
	 * 编辑一个商品图片的信息，根据id
	 * @param isMainStatus
	 * @param images
	 * @return
	 */
	@RequestMapping("/editImagesById")
	@ResponseBody
	public Boolean editImagesById(String isMainStatus, Images images) {
		if(isMainStatus != null && isMainStatus != "" && isMainStatus.equals("on")) {
			images.setIsMain(1);
			productService.editProductImgMainById(images.getF_pid(),images.getImg());
		}else {
			images.setIsMain(0);
		}
		return imagesService.editImagesById(images);
	}
	
	/**
	 * 批量删除商品图片
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delAllImagesByIds")
	@ResponseBody
	public Boolean delAllImagesByIds(@RequestParam("ids") String ids) {
		return imagesService.delAllImagesByIds(ids);
	}
	
	/**
	 * 删除单个的商品图片数据根据id
	 * @param id
	 * @return
	 */
	@RequestMapping("/delImagesById/{id}")
	@ResponseBody
	public Boolean delImagesById(@PathVariable("id") Integer id) {
		return imagesService.delImagesById(id);
	}
	
	/**
	 * 编辑商品图片是否成为主图的状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editImagesIsMainById/{id}/{valid}")
	@ResponseBody
	public Boolean editImagesIsMainById(@PathVariable("id") Integer id,@PathVariable("valid") String valid,String productMainImgUrl,Integer productId) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
			productService.editProductImgMainById(productId,productMainImgUrl);
		}else {
			number = 0;
		}
		return imagesService.editImagesIsMainById(id,number);
	}
	
	/**
	 * 查询出images表里面的是否主图状态为开启
	 * 查询出图片表里的是否是主图的状态为处理图片页面开关是是否可编辑做准备
	 * @param productId
	 * @return
	 */
	@RequestMapping("/queryAllImagesIsMainOnStatusByF_pid/{productId}")
	@ResponseBody
	public Boolean queryAllImagesIsMainOnStatusByF_pid(@PathVariable("productId")Integer productId) {
		return imagesService.queryAllImagesIsMainOnStatusByF_pid(productId);
	}
	
}
